package com.lanyou.cook.entity.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.datadictionary.InterfaceRunResult;
import com.lanyou.cook.service.DataDictionaryService;

@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_COOK_BU_JOB_RUN_RESULT")
public class InterfaceExecutionResult {

	private String id;
	private int version;

	// 这些是冗余字段
	private String fieldName;
	private String fieldSystemId;
	private String fieldSystemName;
	private String interfaceName;
	private String scheduleName;
	private String scheduleId;
	private long kettleJobId;
	private Long slaveServerId;
	// 执行结果
	private String runResult;
	// 执行开始时间
	private Date runBeginTime;
	// 执行结束时间
	private Date runEndTime;
	// 执行时长
	private Integer runTimes;
	// 输出数据量
	private Integer recordQTY;
	// 输入数据量
	private Integer inputQTY;
	// 错误通知者
	private String errSendTo;

	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;

	
	// 这些是从Kettle作业运行日志拷过来的冗余字段
	private Long logBatchId;
	private String channelId;
	private String status;
	private Long linesRead;
	private Long linesWritten;
	private Long linesUpdated;
	private Long linesInput;
	private Long linesOutput;
	private Long linesRejected;
	private Long errors;
	private Date startDate;
	private Date endDate;
	private Date logDate;
	private Date depDate;
	private Date replayDate;
	private String logField;
	private String executingServer;
	private String executingUser;
	private String startjobEntry;
	private String client;
	
	public final static Specification<InterfaceExecutionResult> EmptySpecification = new Specification<InterfaceExecutionResult>(){
		@Override
		public Predicate toPredicate(Root<InterfaceExecutionResult> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<InterfaceExecutionResult> idSpecification(String id){
		return new Specification<InterfaceExecutionResult>(){
			@Override
			public Predicate toPredicate(Root<InterfaceExecutionResult> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "JOB_RUN_RESULT_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Version
	@JsonIgnore
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Column(name = "FIELD_NAME")
	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	
	@Column(name="FIELD_SYSTEM_ID")
	public String getFieldSystemId() {
		return fieldSystemId;
	}

	public void setFieldSystemId(String fieldSystemId) {
		this.fieldSystemId = fieldSystemId;
	}

	@Column(name = "SYSTEM_NAME")
	public String getFieldSystemName() {
		return fieldSystemName;
	}

	public void setFieldSystemName(String fieldSystemName) {
		this.fieldSystemName = fieldSystemName;
	}

	@Column(name = "JOB_NAME")
	public String getInterfaceName() {
		return interfaceName;
	}

	public void setInterfaceName(String interfaceName) {
		this.interfaceName = interfaceName;
	}

	@Column(name = "JOB_SCHEDULE_NAME")
	public String getScheduleName() {
		return scheduleName;
	}

	public void setScheduleName(String scheduleName) {
		this.scheduleName = scheduleName;
	}
	
	@Column(name = "JOB_SCHEDULE_ID")
	public String getScheduleId() {
		return scheduleId;
	}

	public void setScheduleId(String scheduleId) {
		this.scheduleId = scheduleId;
	}

	@Column(name = "R_JOB_ID")
	public long getKettleJobId() {
		return kettleJobId;
	}

	public void setKettleJobId(long kettleJobId) {
		this.kettleJobId = kettleJobId;
	}

	@Column(name="R_SLAVE_ID")
	public Long getSlaveServerId() {
		return slaveServerId;
	}

	public void setSlaveServerId(Long slaveServerId) {
		this.slaveServerId = slaveServerId;
	}

	@Column(name = "RUN_RESULT", length = 10)
	public String getRunResult() {
		return runResult;
	}

	public void setRunResult(String runResult) {
		this.runResult = runResult;
	}

	@Column(name = "RUN_BEGIN_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	public Date getRunBeginTime() {
		return runBeginTime;
	}

	public void setRunBeginTime(Date runBeginTime) {
		this.runBeginTime = runBeginTime;
	}

	@Column(name = "RUN_END_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	public Date getRunEndTime() {
		return runEndTime;
	}

	public void setRunEndTime(Date runEndTime) {
		this.runEndTime = runEndTime;
	}

	@Column(name = "RUN_TIMES")
	public Integer getRunTimes() {
		return runTimes;
	}

	public void setRunTimes(Integer runTimes) {
		this.runTimes = runTimes;
	}

	@Column(name = "RECORD_QTY")
	public Integer getRecordQTY() {
		return recordQTY;
	}

	public void setRecordQTY(Integer recordQTY) {
		this.recordQTY = recordQTY;
	}

	@Column(name = "INPUT_QTY")
	public Integer getInputQTY() {
		return inputQTY;
	}

	public void setInputQTY(Integer inputQTY) {
		this.inputQTY = inputQTY;
	}

	@Column(name = "ERR_SEND_TO", length = 100)
	public String getErrSendTo() {
		return errSendTo;
	}

	public void setErrSendTo(String errSendTo) {
		this.errSendTo = errSendTo;
	}

	@CreatedBy
	@Column(length = 50, updatable = false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable = false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@Column(name="ID_JOB_LOG")
	public Long getLogBatchId() {
		return logBatchId;
	}

	public void setLogBatchId(Long logBatchId) {
		this.logBatchId = logBatchId;
	}

	@Column(name="CHANNEL_ID")
	public String getChannelId() {
		return channelId;
	}

	public void setChannelId(String channelId) {
		this.channelId = channelId;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Column(name="LINES_READ")
	public Long getLinesRead() {
		return linesRead;
	}

	public void setLinesRead(Long linesRead) {
		this.linesRead = linesRead;
	}

	@Column(name="LINES_WRITTEN")
	public Long getLinesWritten() {
		return linesWritten;
	}

	public void setLinesWritten(Long linesWritten) {
		this.linesWritten = linesWritten;
	}

	@Column(name="LINES_UPDATED")
	public Long getLinesUpdated() {
		return linesUpdated;
	}

	public void setLinesUpdated(Long linesUpdated) {
		this.linesUpdated = linesUpdated;
	}

	@Column(name="LINES_INPUT")
	public Long getLinesInput() {
		return linesInput;
	}

	public void setLinesInput(Long linesInput) {
		this.linesInput = linesInput;
	}

	@Column(name="LINES_OUTPUT")
	public Long getLinesOutput() {
		return linesOutput;
	}

	public void setLinesOutput(Long linesOutput) {
		this.linesOutput = linesOutput;
	}

	@Column(name="LINES_REJECTED")
	public Long getLinesRejected() {
		return linesRejected;
	}

	public void setLinesRejected(Long linesRejected) {
		this.linesRejected = linesRejected;
	}

	public Long getErrors() {
		return errors;
	}

	public void setErrors(Long errors) {
		this.errors = errors;
	}

	@Column(name="STARTDATE")
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	@Column(name="ENDDATE")
	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	@Column(name="LOGDATE")
	public Date getLogDate() {
		return logDate;
	}

	public void setLogDate(Date logDate) {
		this.logDate = logDate;
	}

	@Column(name="DEPDATE")
	public Date getDepDate() {
		return depDate;
	}

	public void setDepDate(Date depDate) {
		this.depDate = depDate;
	}

	@Column(name="REPLAYDATE")
	public Date getReplayDate() {
		return replayDate;
	}

	public void setReplayDate(Date replayDate) {
		this.replayDate = replayDate;
	}

	@Column(name="LOG_FIELD")
	@JsonIgnore
	public String getLogField() {
		return logField;
	}

	public void setLogField(String logField) {
		this.logField = logField;
	}

	@Column(name="EXECUTING_SERVER")
	public String getExecutingServer() {
		return executingServer;
	}

	public void setExecutingServer(String executingServer) {
		this.executingServer = executingServer;
	}

	@Column(name="EXECUTING_USER")
	public String getExecutingUser() {
		return executingUser;
	}

	public void setExecutingUser(String executingUser) {
		this.executingUser = executingUser;
	}

	@Column(name="START_JOB_ENTRY")
	public String getStartjobEntry() {
		return startjobEntry;
	}

	public void setStartjobEntry(String startjobEntry) {
		this.startjobEntry = startjobEntry;
	}

	@Column(name="CLIENT")
	public String getClient() {
		return client;
	}

	public void setClient(String client) {
		this.client = client;
	}

	@Transient
	public String getRunResultForDisplay() {
		return DataDictionaryService.getInstance().getValueName(InterfaceRunResult.TypeCode, runResult);
	}

	@Transient
	public String getRunBeginTimeForExport() {
		Date time = getRunBeginTime();
		if(time!=null){
			return time.toString();
		}
		return "";
	}
	
	@Transient
	public String getRunEndTimeForExport() {
		Date time = getRunEndTime();
		if(time!=null){
			return time.toString();
		}
		return "";
	}
	
}
